﻿#pragma execution_character_set("utf-8")
// 本文件为utf-8 编码格式

#include "GdDefine.h"
#include <CrossAppExt.h>
#include <iostream>
#include <map>
#include "CartoonHouse.h"

/* 声明全局变量 */
float design_scale;
bool keyBackEnable=true;
std::string GD_IMAGE_PATH = "normal/images/";
std::string APP_NAME;
std::string APP_NAME_SHORT;
std::string APP_VER;
std::string GD_DOMAIN;

bool IS_DEBUG;
float FONT_SCALE;
GdBean::GdBean(){
	isNotNull = false;
	id = 0;
	strId = "";
	name = "";
	value = "";
	ext = "";
}

GdPage::GdPage(){
	sp = 1;
	totalPages = 0;
	total = 0;
	pageSize = 20;
}

GdPage::GdPage(int sp, int totalPages, int total, int pageSize)
:sp(sp)
, totalPages(totalPages)
, total(total)
, pageSize(pageSize)
{
	
}

bool GdPage::isPageEnd(){
	if (totalPages <= 0 || sp == totalPages){
		return true;
	}
	return false;
}

GDMall::GDMall(){
	isNotNull = false;
}

GDGoodsShort::GDGoodsShort(){
	isNotNull = false;
	is_like = false;
}

GDGoods::GDGoods(){
	isNotNull = false;
	is_like = false;
}

GDSalaryRange::GDSalaryRange(){
	isNotNull = false;
}
GDSharePlatform::GDSharePlatform(){
	isNotNull = false;
}

GDBrand::GDBrand(){
	isNotNull = false;
}

GDFavs::GDFavs(){
	isNotNull = false;
}

GDComment::GDComment(){
	isNotNull = false;
}

GDCity::GDCity(){
	isNotNull = false;
}

GDGoodsGrabSite::GDGoodsGrabSite(){
	isNotNull = false;
}

GDUserShort::GDUserShort(){
	isNotNull = false;
}

GDTopicIcon::GDTopicIcon(){
	isNotNull = false;
}

GDInteresteTags::GDInteresteTags(){
	isNotNull = false;
}

GDSearchCates::GDSearchCates(){
	isNotNull = false;
}

GDGoodsTags::GDGoodsTags(){
	isNotNull = false;
}

GDGoodsIcon::GDGoodsIcon(){
	isNotNull = false;
}

GDTopic::GDTopic(){
	isNotNull = false;
}

GdFilterOrderItem::GdFilterOrderItem(){
	isNotNull = false;
	type = 0;
}

GdFilterOrder::GdFilterOrder(){
	isNotNull = false;
}

GDGoodsGrab::GDGoodsGrab(){
	isNotNull = false;
	is_like = false;
	is_follow = false;
}

std::string GDGoodsGrab::getImage640()
{
	if (img.empty() || img == ""){
		return "";
	}
	return img + "_640x640.jpg";
}

std::string GDGoodsGrab::getImage640f()
{
	if (img.empty() || img == ""){
		return "";
	}
	return img + "_640x640_f.jpg";
}

bool GDCity::hasParentCode(){
	if (parentCode.empty() || parentCode == "" || parentCode == "0"){
		return false;
	}
	return true;
}

std::string GDUserShort::getDarenTitle(){
	std::string title = "普通会员";

	if (isNotNull){
		if (daren == "1"){
			title = "买手";
		}
		else if (daren == "2"){
			title = "达人";
		}
	}
	else{
		title = "匿名用户";
	}
	

	return title;
}

bool GDGoodsShort::isSale(){
	if (!this->type.empty() && this->type.length() > 0){
		int iType = atoi(this->type.c_str());
		if (iType == 2){
			return true;
		}
	}
	return false;
}

bool GDGoodsShort::isGrab(){
	if (!this->type.empty() && this->type.length() > 0){
		int iType = atoi(this->type.c_str());
		if (iType == 4){
			return true;
		}
	}
	return false;
}

std::string GDGoodsShort::getSaleInfo(){
	if (sale_type.empty() || sale_type == "" || sale_type == "0"){
		return "";
	}
	if (sale_type == "3"){
		return "接受定制或预约";
	}
	else if (sale_type == "2"){ // 限量
		if (left_num == "0"){
			return "限量：已售完";			
		}
		else{
			return "限量：" + left_num + "件";
		}
	}

	std::string saleInfo = "";

	if (expire_time.length() > 0){ // 如果有过期时间
		saleInfo = "限时：";
		time_t now = time(NULL);
		time_t t = std::atoi(expire_time.c_str());
		if (now > t){
			saleInfo = saleInfo + "活动已结束";
		}
		else{
			long diff = (long)difftime(t,now);
			int day = diff / ONE_DAY_SEC; // 天数
			if (day > 0){
				saleInfo = saleInfo + CCString::createWithFormat("%d天 ", day)->m_sString;
				diff = diff % ONE_DAY_SEC;
			}

			time_t t = time_t(diff);
			struct tm *p = gmtime(&t);
			char s[80];
			strftime(s, 80, "%H:%M:%S", p);
			saleInfo = saleInfo + s;

			if (left_num.length() > 0 && left_num != "0"){ // 如果有数量		
				if (saleInfo.length() > 0){
					saleInfo = saleInfo + " ";
				}
				saleInfo = saleInfo + "限量：" + left_num + "件";
			}
		}
	}
	
	return saleInfo;
}

bool GDGoods::isSale(){
	if (!this->type.empty() && this->type.length() > 0){
		int iType = atoi(this->type.c_str());
		if (iType == 2){
			return true;
		}
	}
	return false;
}

bool GDGoods::isGrab(){
	if (!this->type.empty() && this->type.length() > 0){
		int iType = atoi(this->type.c_str());
		if (iType == 4){
			return true;
		}
	}
	return false;
}

std::string GDGoods::getSaleInfo(){
	if (sale_type.empty() || sale_type == "" || sale_type == "0"){
		return "";
	}
	if (sale_type == "3"){
		return "接受定制或预约";
	}
	else if (sale_type == "2"){ // 限量
		if (left_num == "0"){
			return "限量：已售完";
		}
		else{
			return "限量：" + left_num + "件";
		}
	}

	std::string saleInfo = "";

	if (expire_time.length() > 0){ // 如果有过期时间
		saleInfo = "限时：";
		time_t now = time(NULL);
		time_t t = std::atoi(expire_time.c_str());
		if (now > t){
			saleInfo = saleInfo + "活动已结束";
		}
		else{
			long diff = (long)difftime(t, now);
			int day = diff / ONE_DAY_SEC; // 天数
			if (day > 0){
				saleInfo = saleInfo + CCString::createWithFormat("%d天 ", day)->m_sString;
				diff = diff % ONE_DAY_SEC;
			}

			time_t t = time_t(diff);
			struct tm *p = localtime(&t);
			char s[80];
			strftime(s, 80, "%H:%M:%S", p);
			saleInfo = saleInfo + s;

			if (left_num.length() > 0 && left_num != "0"){ // 如果有数量		
				if (saleInfo.length() > 0){
					saleInfo = saleInfo + " ";
				}
				saleInfo = saleInfo + "限量：" + left_num + "件";
			}
		}
	}

	return saleInfo;
}

GdBean parseGdBean(const CSJson::Value &value, std::string idKey, std::string strIdKey, std::string nameKey, std::string valueKey, std::string extKey){
	if (idKey.empty() || idKey == "") idKey = "id";
	if (strIdKey.empty() || strIdKey == "") strIdKey = "strId";
	if (nameKey.empty() || nameKey == "") nameKey = "name";
	if (valueKey.empty() || valueKey == "") valueKey = "value";
	if (extKey.empty() || extKey == "") extKey = "ext";

	GdBean bean;
	bean.isNotNull = true;
	if (value.isMember(idKey)){
		bean.id = value[idKey].asInt();
	}
	if (value.isMember(strIdKey)){
		if (value[strIdKey].isInt()){
			bean.strId = CCString::createWithFormat("%d", value[strIdKey].asInt())->m_sString;
		}
		else{
			bean.strId = value[strIdKey].asString();
		}
	}
	if (value.isMember(nameKey)){
		bean.name = value[nameKey].asString();
	}
	if (value.isMember(valueKey)){
		bean.value = value[valueKey].asString();
	}
	if (value.isMember(extKey)){
		bean.ext = value[extKey].asString();
	}
	
	return bean;
}

GdFilterOrderItem parseGdFilterOrderItem(const CSJson::Value &value){
	GdFilterOrderItem bean;

	bean.isNotNull = true;
	bean.p = value["p"].asString();
	bean.name = value["name"].asString();
	if (value.isMember("type")){
		bean.type = value["type"].asInt();
	}
	if (value.isMember("items")){
		const CSJson::Value &itemList = value["items"];

		for (int i = 0; i < itemList.size(); i++) {
			GdBean item = parseGdBean(itemList[i]);
			bean.items.push_back(item);
		}
	}
	return bean;
}

GdFilterOrder parseGdFilterOrder(const CSJson::Value &value){
	GdFilterOrder bean;
	bean.isNotNull = true;

	if (value.isMember("filter")){
		const CSJson::Value &itemList = value["filter"];

		for (int i = 0; i < itemList.size(); i++) {
			GdFilterOrderItem item = parseGdFilterOrderItem(itemList[i]);
			bean.filter.push_back(item);
		}
	}

	if (value.isMember("order")){
		bean.order = parseGdFilterOrderItem(value["order"]);
	}

	return bean;
}

GDGoods* parseGDGoods(const CSJson::Value &json){
	GDGoods goods;

	parseGDGoods(goods, json);
	return &goods;
}

void parseGDGoods(GDGoods& goods, const CSJson::Value &json){
	const CSJson::Value &value = json["detail"];
	if (value.empty()){ // 如果没有detail数据
		return;
	}

	goods.isNotNull = true;
	goods._id = value["id"].asString();
	goods.uid = value["uid"].asString();
	goods.index_desc = value["index_desc"].asString();
	goods.desc = value["describe"].asString();
	goods.info = value["info"].asString();
	goods.flow = value["favs"].asString();
	goods.pic = value["img"].asString();
	goods.img_color = value["img_color"].asString();
	goods.url = value["url"].asString();
	goods.title = value["title"].asString();
	goods.price = value["prices"].asString();
	goods.market_price = value["market_price"].asString();
	goods.rmb_price = value["rmb_price"].asString();
	goods.purpose = value["purpose"].asString(); // 适合对象及用途
	goods.prices_analyse = value["prices_analyse"].asString(); // 价格分析
	goods.logistics = value["logistics"].asString(); // 物流支付
	goods.score = value["score"].asString(); // 果冻打分
	goods.product_area = value["product_area"].asString();
	goods.uname = value["uname"].asString();
	goods.hits = value["hits"].asString();
	goods.currency_type = value["currency_type"].asString();
	goods.buy_tips = value["buy_tips"].asString();
	goods.comment_count = value["comments"].asString();
	goods.coupon_code = value["coupon_code"].asString();
	goods.jd_imgs[0] = value["jd_img1"].asString();
	goods.jd_imgs[1] = value["jd_img2"].asString();
	goods.jd_imgs[2] = value["jd_img3"].asString();
	goods.jd_imgs[3] = value["jd_img4"].asString();
	goods.jd_imgs[4] = value["jd_img5"].asString();
	goods.favs = value["favs"].asString();
	goods.mall_id = value["mall_id"].asString();
	goods.brand_id = value["brand_id"].asString();
	goods.type = value["type"].asString();
	
	if (value.isMember("is_like")){
		goods.is_like = value["is_like"].asBool();
	}
	else{
		goods.is_like = false;
	}
	if (value.isMember("is_follow")){
		goods.is_follow = value["is_follow"].asBool();
	}
	else{
		goods.is_follow = false;
	}
	
	if (value.isMember("post_time")){
		goods.post_time = value["post_time"].asString();
	}
	else{
		goods.post_time = "";
	}

	if (value.isMember("sale_type")){
		goods.sale_type = value["sale_type"].asString();
	}
	else{
		goods.sale_type = "";
	}
	if (value.isMember("expire_time")){
		goods.expire_time = value["expire_time"].asString();
	}
	else{
		goods.expire_time = "";
	}
	if (value.isMember("left_num")){
		goods.left_num = value["left_num"].asString();
	}
	else{
		goods.left_num = "0";
	}
	
	if (!value["mall"].empty()){
		goods.mall = parseGDMall(value["mall"]);
	}
	else{
		goods.mall = GDMall();
	}
	
	if (!value["brand"].empty()){
		goods.brand = parseGDBrand(value["brand"]);
	}
	else{
		goods.brand = GDBrand();
	}

	if (!value["user"].empty()){
		goods.userShort = parseGDUserShort(value["user"]);
	}
	
	goods.goodsIcons.clear();
	if (!value["icon_list"].empty()){
		const CSJson::Value &iconList = value["icon_list"];

		for (int i = 0; i < iconList.size(); i++) {
			GDGoodsIcon icon = parseGDGoodsIcon(iconList[i]);
			goods.goodsIcons.push_back(icon);
		}
	}

	goods.goodsTags.clear();
	if (!value["tag_list"].empty()){
		const CSJson::Value &tagList = value["tag_list"];

		for (int i = 0; i < tagList.size(); i++) {
			GDGoodsTags tag = parseGDGoodsTags(tagList[i]);
			goods.goodsTags.push_back(tag);
		}
	}

	if (!value["hot_tags"].empty() && value["hot_tags"].isArray()){
		const CSJson::Value &hotTagsList = value["hot_tags"];

		for (int i = 0; i < hotTagsList.size(); i++) {
			goods.hotTags.push_back(parseGdBean(hotTagsList[i], "iid", "id", "title"));
		}
	}

	if (!value["tags"].empty()){
		const CSJson::Value &tagsList = value["tags"];

		for (int i = 0; i < tagsList.size(); i++) {
			GDGoodsGrabMark *goodsGrabMark = parseGDGoodsGrabMark(tagsList[i]);
			goods.tags.push_back(goodsGrabMark);
		}
	}

	goods.commentList.clear();
	if (!json["comment_list"].empty()){
		const CSJson::Value &commentsList = json["comment_list"];

		for (int i = commentsList.size()-1; i >=0; i--) {
			GDComment itemComment = parseGDComment(commentsList[i]);
			goods.commentList.push_back(itemComment);
		}
	}
	
	goods.favsList.clear();
	if (!json["favs_users"].empty()){
		const CSJson::Value &favsList = json["favs_users"];

		for (int i = 0; i < favsList.size(); i++) {
			GDFavs item = parseGDFavs(favsList[i]);
			goods.favsList.push_back(item);
		}
	}

	goods.similarGoodsList.clear();
	if (!json["like_list"].empty()){
		const CSJson::Value &commentsList = json["like_list"];

		for (int i = 0; i < commentsList.size(); i++) {
			GDHotLabel itemComment = parseGDHotLabel(commentsList[i]);
			goods.similarGoodsList.push_back(itemComment);
		}
	}

	goods.recommendGoodsList.clear();
	if (!json["ta_post_list"].empty()){
		const CSJson::Value &commentsList = json["ta_post_list"];

		for (int i = 0; i < commentsList.size(); i++) {
			GDHotLabel itemComment = parseGDHotLabel(commentsList[i]);
			goods.recommendGoodsList.push_back(itemComment);
		}
	}
}

GDTopic parseGDTopic(const CSJson::Value &value){
	GDTopic topic;
	topic.isNotNull = true;
	topic.id = value["id"].asString();
	topic.title = value["title"].asString();
	topic.img = value["img"].asString();
	topic.list_img = value["list_img"].asString();
	topic.uname = value["uname"].asString();
	topic.favs = value["favs"].asString();
	topic.hits = value["hits"].asString();
	topic.uid = value["uid"].asString();
	topic.img_color = value["img_color"].asString();
	topic.describe = value["describe"].asString();
	topic.post_time = value["post_time"].asString();
	
	if (value.isMember("topic_icon")){
		topic.topic_icon = value["topic_icon"].asString();
	}
	if (value.isMember("user")){
		topic.user = parseGDUserShort(value["user"]);
	}
	if (value.isMember("mall")){
		topic.mall = parseGDMall(value["mall"]);
	}
	if (value.isMember("brand")){
		topic.brand = parseGDBrand(value["brand"]);
	}
	return topic;
}

GDGoodsShort parseGDGoodsShort(const CSJson::Value &value){
	GDGoodsShort goods;
	goods.isNotNull = true;
	goods._id = value["id"].asString();
	goods.uid = value["uid"].asString();
	goods.index_desc = value["index_desc"].asString();
	goods.flow = value["favs"].asString();
	goods.favs = value["favs"].asString();
	goods.pic = value["img"].asString();
	goods.img_color = value["img_color"].asString();
	goods.title = value["title"].asString();
	goods.price = value["prices"].asString();
	goods.market_price = value["market_price"].asString();
	goods.score = value["score"].asString(); // 果冻打分
	goods.hits = value["hits"].asString();
	goods.currency_type = value["currency_type"].asString();
	goods.describe = value["describe"].asString();
    goods.type = value["type"].asString();
	goods.prices_analyse = value["prices_analyse"].asString();
	goods.jd_img1 = value["jd_img1"].asString();
	goods.jd_img2 = value["jd_img2"].asString();
	goods.jd_img3 = value["jd_img3"].asString();
    goods.uname = value["uname"].asString();
	
	if (value.isMember("post_time")){
		goods.post_time = value["post_time"].asString();
	}

	if (value.isMember("sale_type")){
		goods.sale_type = value["sale_type"].asString();
	}
	if (value.isMember("expire_time")){
		goods.expire_time = value["expire_time"].asString();
	}
	if (value.isMember("left_num")){
		goods.left_num = value["left_num"].asString();
	}

	if (value.isMember("is_like")){
		goods.is_like = value["is_like"].asBool();
	}
	
	if (!value["mall"].empty()){
		goods.mall = parseGDMall(value["mall"]);
	}

	if (!value["icon_list"].empty()){
		const CSJson::Value &iconList = value["icon_list"];

		for (int i = 0; i < iconList.size(); i++) {
			GDGoodsIcon icon = parseGDGoodsIcon(iconList[i]);
			goods.goodsIcons.push_back(icon);
		}
	}

	return goods;
}

GDMall parseGDMall(const CSJson::Value &value)
{
	GDMall mall;
	mall.isNotNull = true;
	mall._id = value["id"].asString();
	mall.title = value["title"].asString(); // 标题
	mall.info = value["info"].asString(); // 信息
	mall.country = value["country"].asString(); // 产地
	mall._img = value["_img"].asString(); // 图片
	mall.order_desc = value["order_desc"].asString(); // 
	
	return mall;
}

std::string domain;
std::string abst;
std::string country;
std::string info;

GDBrand parseGDBrand(const CSJson::Value &value){
	GDBrand brand;
	brand.isNotNull = true;
	brand._id = value["id"].asString();
	brand.title = value["title"].asString(); // 标题
	brand.domain = value["domain"].asString(); // 网址
	brand.country = value["country"].asString(); // 国家
	brand.abst = value["abst"].asString(); // 简介
	brand.info = value["info"].asString(); // 图片信息
	return brand;
}

GDComment parseGDComment(const CSJson::Value &value)
{
	GDComment comment;
	comment.isNotNull = true;
	comment._id = value["id"].asString();
	comment.uname = value["username"].asString();
	comment.info = value["info"].asString();
	comment.sendTime = value["add_time"].asString();
	comment.client = value["client"].asString();
	if (value.isMember("uid")){
		if (value["uid"].isString()){
			comment.uid = value["uid"].asString();
		}
		if (value["uid"].isInt()){
			comment.uid = CCString::createWithFormat("%d",value["uid"].asInt())->m_sString;
		}
	}
	if (value.isMember("to_user")){
		comment.toUser = parseGDUserShort(value["to_user"]);
	}
	else{
		comment.toUser.isNotNull = false;
	}
	return comment;
}

void parseGDPage(GdPage& page, const CSJson::Value &value)
{
	if (value.isMember("sp")){
		page.sp = value["sp"].asInt();
	}
	if (value.isMember("totalPages")){
		page.totalPages = value["totalPages"].asInt();
	}
	if (value.isMember("pageSize")){
		page.pageSize = value["pageSize"].asInt();
	}
	if (value.isMember("total")){
		if (value["total"].isString()){
			page.total = atoi(value["total"].asString().c_str());
		}
		else if (value["total"].isInt()){
			page.total = value["total"].asInt();
		}
	}
}

GDFavs parseGDFavs(const CSJson::Value &value){
	GDFavs favs;
	favs.isNotNull = true;
	favs._id = value["id"].asString();
	favs.addTime = value["add_time"].asString();
	if (!value["user"].empty()){
		favs.userShort = parseGDUserShort(value["user"]);
	}
	return favs;
}

GDCity parseGDCity(const CSJson::Value &value){
	GDCity city;
	city.isNotNull = true;
	city.code = value["code"].asString();
	city.name = value["name"].asString();
	city.parentCode = value["parentCode"].asString();
	return city;
}

GDSharePlatform parseGDSharePlatform(const CSJson::Value &value){
	GDSharePlatform city;
	city.isNotNull = true;
	city.id = value["id"].asString();
	city.name = value["name"].asString();
	city.icon = value["icon"].asString();
	return city;
}

GDGoodsGrabSite parseGDGoodsGrabSite(const CSJson::Value &value){
	GDGoodsGrabSite goodsGrabSite;
	goodsGrabSite.isNotNull = true;
	goodsGrabSite.id = value["id"].asString();
	goodsGrabSite.name = value["name"].asString();
	goodsGrabSite.url = value["url"].asString();
	return goodsGrabSite;
}

GDTopicIcon parseGDTopicIcon(const CSJson::Value &value){
	GDTopicIcon topicIcon;
	topicIcon.isNotNull = true;
	topicIcon.id = value["id"].asString();
	topicIcon.name = value["name"].asString();
	topicIcon.color = value["color"].asString();
	return topicIcon;
}

GDImageFilter::GDImageFilter()
{
	isNotNull=false; // 默认为空
	id=0; // id
	name=""; // 标题
	value="";
	bright=0; //亮度
	contrast=0; // 对比度
	saturation=0; // 饱和度
	hue=0; // 色相
	cbr = 0;
	cbg = 0;
	cbb = 0;
}

GDImageFilter parseGDImageFilter(const CSJson::Value &value)
{
	GDImageFilter imageFilter = GDImageFilter();
	imageFilter.isNotNull = true;
	imageFilter.id = value["id"].asInt();
	imageFilter.name = value["name"].asString();
	imageFilter.value = value["value"].asString();
	imageFilter.bright = value["bright"].asFloat();
	imageFilter.contrast = value["contrast"].asFloat();
	imageFilter.saturation = value["saturation"].asFloat();
	imageFilter.hue = value["hue"].asInt();
	imageFilter.cbr = value["cbr"].asInt();
	imageFilter.cbg = value["cbg"].asInt();
	imageFilter.cbb = value["cbb"].asInt();

	return imageFilter;
}


GDSalaryRange parseGDSalaryRange(const CSJson::Value &value){
	GDSalaryRange salaryRange = GDSalaryRange();
	salaryRange.isNotNull = true;
	salaryRange._id = value["id"].asString();
	salaryRange.name = value["name"].asString();
	return salaryRange;
}

GDUserShort parseGDUserShort(const CSJson::Value &value){
	GDUserShort userShort = GDUserShort();
	userShort.isNotNull = true;
	userShort._id = value["id"].asString();
	userShort.username = value["username"].asString();
	userShort.daren = value["daren"].asString();
	userShort.fans = value["fans"].asString();
	userShort.shares = value["shares"].asString();

	return userShort;
}

GDInteresteTags parseGDInteresteTags(const CSJson::Value &value){
	GDInteresteTags userShort = GDInteresteTags();
	userShort.isNotNull = true;
	userShort.id = value["id"].asString();
	userShort.name = value["name"].asString();
	userShort.ordid = value["ordid"].asString();

	return userShort;
}

GDSearchCates parseGDSearchCates(const CSJson::Value &value){
	GDSearchCates searchCates = GDSearchCates();

	searchCates.isNotNull = true;
	searchCates.id = value["id"].asString();
	searchCates.name = value["name"].asString();
	searchCates.ordid = value["ordid"].asString();
	if (value.isMember("depth")){
		searchCates.depth = value["depth"].asInt();
	}
	
    if (value.isMember("img")){
        searchCates.img = value["img"].asString();
    }
    
    searchCates.type = value.isMember("type") ? value["type"].asInt() : 0;

	if (value.isMember("child")){ // 如果有子元素
		const CSJson::Value &subList = value["child"];

		for (int i = 0; i < subList.size(); i++) {
			GDSearchCates subSearchCates = parseGDSearchCates(subList[i]);
			searchCates.subCates.push_back(subSearchCates);
		}
	}
	return searchCates;
}


GDGoodsTags parseGDGoodsTags(const CSJson::Value &value){
	GDGoodsTags userShort = GDGoodsTags();
	userShort.isNotNull = true;
	userShort.id = value["id"].asString();
	userShort.name = value["name"].asString();

	return userShort;
}

GDGoodsIcon parseGDGoodsIcon(const CSJson::Value &value){
	GDGoodsIcon userShort = GDGoodsIcon();
	userShort.isNotNull = true;
	userShort.id = value["id"].asString();
	userShort.title = value["title"].asString();

	return userShort;
}

GDGoodsGrab parseGDGoodsGrab(const CSJson::Value &value)
{
	//CCLog("goodsGrab:%s", value.toStyledString().c_str());
	GDGoodsGrab goodsGrab = GDGoodsGrab();
	goodsGrab.isNotNull = true;
	goodsGrab._id = value["id"].asString();
	goodsGrab.uid = value["uid"].asString();
	goodsGrab.title = value["title"].asString();
	goodsGrab.type = value["type"].asString();
	goodsGrab.img = value["img"].asString();
	goodsGrab.img_color = value["img_color"].asString();
	goodsGrab.price = value["prices"].asString();
	goodsGrab.market_price = value["market_price"].asString();
	goodsGrab.flow = value["flow"].asString();
	goodsGrab.favs = value["favs"].asString();
	goodsGrab.currency_type = value["currency_type"].asString();
	goodsGrab.post_time = value["post_time"].asString();
	goodsGrab.rmb_price = value["rmb_price"].asString();
	goodsGrab.uname = value["uname"].asString();
	goodsGrab.hits = value["hits"].asString();
	goodsGrab.comments = value["comments"].asString();
	goodsGrab.desc = value["describe"].asString();

	if (value.isMember("is_like")){
		goodsGrab.is_like = value["is_like"].asBool();
	}
	if (value.isMember("is_follow")){
		goodsGrab.is_follow = value["is_follow"].asBool();
	}

	if (!value["favs_users"].empty()){
		const CSJson::Value &favsList = value["favs_users"];

		for (int i = 0; i < favsList.size(); i++) {
			GDFavs item = parseGDFavs(favsList[i]);
			goodsGrab.favsUser.push_back(item);
		}
	}

	if (!value["tags"].empty()){
		const CSJson::Value &tagsList = value["tags"];

		for (int i = 0; i < tagsList.size(); i++) {
			GDGoodsGrabMark *goodsGrabMark = parseGDGoodsGrabMark(tagsList[i]);
			goodsGrab.tags.push_back(goodsGrabMark);
		}
	}

	if (!value["hot_tags"].empty()){
		if (value["hot_tags"].isArray()){
			const CSJson::Value &hotTagsList = value["hot_tags"];

			for (int i = 0; i < hotTagsList.size(); i++) {
				goodsGrab.hotTags.push_back(parseGdBean(hotTagsList[i], "iid", "id", "title"));
			}
		}
	}

	return goodsGrab;
}

GDGoodsGrabMark::GDGoodsGrabMark()
{
	isNotNull = false;
	pointMove = false;
	id = 0;
	colorType = 0;
	x = 0;
	y = 0;
	x1 = 0;
	y1 = 0;
	x2 = 0;
	y2 = 0;
	x3 = 0;
	y3 = 0;
	rotation1 = 0;
	rotation2 = 0;
	rotation3 = 0;
	id1 = "";
	name1 = "";
	title1 = "";
	id2 = "";
	name2 = "";
	title2 = "";
	id3 = "";
	name3 = "";
	title3 = "";
}

bool GDGoodsGrabMark::brandIsNull(){
	if (((name1.empty() || name1 == "" || name1.length() == 0)) && ((title1.empty() || title1 == "" || title1.length() == 0))){
		return true;
	}
	return false;
}

bool GDGoodsGrabMark::currencyIsNull()
{
	if (((name2.empty() || name2 == "" || name2.length() == 0)) && ((title2.empty() || title2 == "" || title2.length() == 0))){
		return true;
	}
	return false;
}
bool GDGoodsGrabMark::countryIsNull()
{
	if (((name3.empty() || name3 == "" || name3.length() == 0)) && ((title3.empty() || title3 == "" || title3.length() == 0))){
		return true;
	}
	return false;
}

bool GDGoodsGrabMark::isNull()
{
	if (brandIsNull() && currencyIsNull() && countryIsNull()){
		return true;
	}
	return false;
}

std::string GDGoodsGrabMark::getMarkInfo(int type){
	std::string info = "";
	if (type == 1){
		if (!name1.empty()) info = name1;
		if (!title1.empty()){
			if (!info.empty()){
				info = info + "　";
			}
			info = info + title1;
		}
	}
	else if (type == 2){
		if (!name2.empty()) info = name2;
		if (!title2.empty()){
			if (!info.empty()){
				info = info + "　";
			}
			info = info + title2;
		}
	}
	else if (type == 3){
		if (!name3.empty()) info = name3;
		if (!title3.empty()){
			if (!info.empty()){
				info = info + "　";
			}
			info = info + title3;
		}
	}
	return info;
}

CCPoint GDGoodsGrabMark::getMarkPoint(int type){
	if (type == 1){
		return CCPoint(_ds(x1),_ds(y1));
	}
	else if (type == 2){
		return CCPoint(_ds(x2), _ds(y2));
	}
	else if (type == 3){
		return CCPoint(_ds(x3), _ds(y3));
	}
	else {
		return CCPoint(_ds(x), _ds(y));
	}
}

CCPoint GDGoodsGrabMark::getMarkPointNoDs(int type)
{
	if (type == 1){
		return CCPoint((x1), (y1));
	}
	else if (type == 2){
		return CCPoint((x2), (y2));
	}
	else if (type == 3){
		return CCPoint((x3), (y3));
	}
	else {
		return CCPoint((x), (y));
	}
}

void GDGoodsGrabMark::setMarkPoint(int type,const CCPoint& point){
	if (type == 1){
		x1 =_xds(point.x);
		y1 = _xds(point.y);
	}
	else if (type == 2){
		x2 = _xds(point.x);
		y2 = _xds(point.y);
	}
	else if (type == 3){
		x3 = _xds(point.x);
		y3 = _xds(point.y);
	}
	else {
		x = _xds(point.x);
		y = _xds(point.y);
	}
}

void GDGoodsGrabMark::setMarkPoint(int type, float x, float y)
{
	if (type == 1){
		x1 = _xds(x);
		y1 = _xds(y);
	}
	else if (type == 2){
		x2 = _xds(x);
		y2 = _xds(y);
	}
	else if (type == 3){
		x3 = _xds(x);
		y3 = _xds(y);
	}
	else{
		this->x = _xds(x);
		this->y = _xds(y);
	}
}

void GDGoodsGrabMark::setMarkPointNoDs(int type, const CCPoint& point)
{
	if (type == 1){
		x1 = (point.x);
		y1 = (point.y);
	}
	else if (type == 2){
		x2 = (point.x);
		y2 = (point.y);
	}
	else if (type == 3){
		x3 = (point.x);
		y3 = (point.y);
	}
	else {
		x = (point.x);
		y = (point.y);
	}
}

void GDGoodsGrabMark::setMarkPointNoDs(int type, float x, float y)
{
	if (type == 1){
		x1 = (x);
		y1 = (y);
	}
	else if (type == 2){
		x2 = (x);
		y2 = (y);
	}
	else if (type == 3){
		x3 = (x);
		y3 = (y);
	}
	else{
		this->x = (x);
		this->y = (y);
	}
}

int GDGoodsGrabMark::getRotation(int type)
{
	if (type == 1){
		return rotation1;
	}
	else if (type == 2){
		return rotation2;
	}
	else if (type == 3){
		return rotation3;
	}
}
void GDGoodsGrabMark::setRotation(int type, int value)
{
	if (type == 1){
		rotation1 = value;
	}
	else if (type == 2){
		rotation2 = value;
	}
	else if (type == 3){
		rotation3 = value;
	}
}

void GDGoodsGrabMark::deleteValue()
{
	x = 0;
	y = 0;
	x1 = 0;
	y1 = 0;
	x2 = 0;
	y2 = 0;
	x3 = 0;
	y3 = 0;
	rotation1 = 0;
	rotation2 = 0;
	rotation3 = 0;
	id1 = "";
	name1 = "";
	title1 = "";
	id2 = "";
	name2 = "";
	title2 = "";
	id3 = "";
	name3 = "";
	title3 = "";
}

std::string GDGoodsGrabMark::toJsonString()
{
	if (isNull()){
		return "";
	}

	std::string jsonStr = "{";

	jsonStr = jsonStr + CCString::createWithFormat( "\"id\":%d",id)->m_sString ;
	jsonStr = jsonStr + CCString::createWithFormat(",\"color\":%d", colorType)->m_sString;
	jsonStr = jsonStr + ",\"items\":[";

	std::string item = "";
	if (!brandIsNull()){ // 如果有商城数据
		item = "{";
		item = item + "\"type\":1";
		item = item + ",\"id\":\""+id1+"\"";
		if (rotation1 > 0){
			item = item + CCString::createWithFormat(",\"rotation\":%d", rotation1)->m_sString;
		}

		if (!name1.empty()){
			item = item + ",\"name\":\"" + name1 + "\"";
		}
		if (!title1.empty()){
			item = item + ",\"title\":\"" + title1 + "\"";
		}
		item = item + CCString::createWithFormat(",\"point\":[%f,%f]", x1,y1)->m_sString;
		item = item + "}";
	}

	if (!currencyIsNull()){ // 如果有货币数据
		if (!item.empty()){
			item = item + ",";
		}
		item = item + "{";
		item = item + "\"type\":2";
		item = item + ",\"id\":\"" + id2 + "\"";
		if (rotation2 > 0){
			item = item + CCString::createWithFormat(",\"rotation\":%d", rotation2)->m_sString;
		}
		if (!name2.empty()){
			item = item + ",\"name\":\"" + name2 + "\"";
		}
		if (!title2.empty()){
			item = item + ",\"title\":\"" + title2+ "\"";
		}
		item = item + CCString::createWithFormat(",\"point\":[%f,%f]", x2, y2)->m_sString;
		item = item + "}";
	}

	if (!countryIsNull()){ // 如果有国家数据
		if (!item.empty()){
			item = item + ",";
		}
		item = item + "{";
		item = item + "\"type\":3";
		item = item + ",\"id\":\"" + id3 + "\"";
		if (rotation3 > 0){
			item = item + CCString::createWithFormat(",\"rotation\":%d", rotation3)->m_sString;
		}
		if (!name3.empty()){
			item = item + ",\"name\":\"" + name3 + "\"";
		}
		if (!title3.empty()){
			item = item + ",\"title\":\"" + title3 + "\"";
		}
		item = item + CCString::createWithFormat(",\"point\":[%f,%f]", x3, y3)->m_sString;
		item = item + "}";
	}
	jsonStr = jsonStr + item;

	jsonStr = jsonStr + "]";
	jsonStr = jsonStr + "}";
	return jsonStr;
}

GDGoodsGrabMark* parseGDGoodsGrabMark(const CSJson::Value &value)
{
	GDGoodsGrabMark *goodsGrabMark = new GDGoodsGrabMark();
	goodsGrabMark->isNotNull = true;
	goodsGrabMark->pointMove = false;

	//CCLog("parseGDGoodsGrabMark=%s", value.toStyledString().c_str());

	if (value.isMember("id")){
		goodsGrabMark->id = value["id"].asInt();
	}
	goodsGrabMark->colorType = value["color"].asInt();
	
	if (value.isMember("items")){
		const CSJson::Value &itemList = value["items"];

		for (int i = 0; i < itemList.size(); i++) {
			CSJson::Value item = itemList[i];
			int type = item["type"].asInt();
			if (type == 1){
				if (item.isMember("id")) goodsGrabMark->id1 = item["id"].asString();
				if (item.isMember("name")) goodsGrabMark->name1 = item["name"].asString();
				if (item.isMember("title")) goodsGrabMark->title1 = item["title"].asString();
				if (item.isMember("point")){
					const CSJson::Value &point = item["point"];
					goodsGrabMark->setMarkPointNoDs(type, point[0].asFloat(), point[1].asFloat());
				}
				if (item.isMember("rotation")){
					if (item["rotation"].asInt()){
						goodsGrabMark->rotation1 = item["rotation"].asInt();
					}
					else{
						goodsGrabMark->rotation1 = atoi(item["rotation"].asString().c_str());
					}
				}
			}
			else if (type == 2){
				if (item.isMember("id")) goodsGrabMark->id2 = item["id"].asString();
				if (item.isMember("name")) goodsGrabMark->name2 = item["name"].asString();
				if (item.isMember("title")) goodsGrabMark->title2 = item["title"].asString();
				if (item.isMember("point")){
					const CSJson::Value &point = item["point"];
					goodsGrabMark->setMarkPointNoDs(type, point[0].asFloat(), point[1].asFloat());
				}
				if (item.isMember("rotation")){
					if (item["rotation"].asInt()){
						goodsGrabMark->rotation2 = item["rotation"].asInt();
					}
					else{
						goodsGrabMark->rotation2 = atoi(item["rotation"].asString().c_str());
					}
				}
			}
			else if (type == 3){
				if (item.isMember("id")) goodsGrabMark->id3 = item["id"].asString();
				if (item.isMember("name")) goodsGrabMark->name3 = item["name"].asString();
				if (item.isMember("title")) goodsGrabMark->title3 = item["title"].asString();
				if (item.isMember("point")){
					const CSJson::Value &point = item["point"];
					goodsGrabMark->setMarkPointNoDs(type, point[0].asFloat(), point[1].asFloat());
				}
				if (item.isMember("rotation")){
					if (item["rotation"].asInt()){
						goodsGrabMark->rotation3 = item["rotation"].asInt();
					}
					else{
						goodsGrabMark->rotation3 = atoi(item["rotation"].asString().c_str());
					}
				}
			}
		}
	}

	return goodsGrabMark;
}

std::string getFullUrl(string url){
	string returnUrl = url;
	if (strncmp(url.c_str(), "http", strlen("http")) != 0){ // 如果url不是以http开头
//		returnUrl = GD_DOMAIN + url;
	}
	// CCLog("srcouseUrl.%s  \n returnUrl.%s", url.c_str(), returnUrl.c_str());
	return returnUrl;
}

float _ds(float px){
	if (px == 0){
		return 0;
	}
	else if (px == 1){
		return 1;
	}
	else{
		return px * design_scale;
	}
}

float _xds(float px){
	return px / design_scale;
}

std::string formatDateTime(const string seconds){
	time_t nowtime = seconds.empty() ? time(NULL) : std::atoi(seconds.c_str());
	struct tm *local = localtime(&nowtime);
	char *buffer = new char[12];
	strftime(buffer, 128, "%c", local);
	cout << buffer << endl;
	return buffer;
}


GDMainHotLabel parseGDMainHotLabel(const CSJson::Value &value){
    GDMainHotLabel model;
    model.id    = value["id"].asString();
    model.title = value["title"].asString();
    model.type  = value["type"].asString();
    model.image = value["logo"].asString();
    
    if (value.isMember("items")) {
        const CSJson::Value& items =  value["items"];
        if (!items.empty() && items.size() != 0) {
            for (int i = 0; i < items.size(); i++) {
                const CSJson::Value& itemValue = items[i];
                GDMainHotLabel subModel;
                subModel.id    = itemValue["id"].asString();
                subModel.image = itemValue["img"].asString();
                model.subLabel.push_back(subModel);
            }
        }
    }
    return model;
}


GDHotLabel parseGDHotLabel(const CSJson::Value &value){
    GDHotLabel model;
    model.id       = value["id"].asString();
    model.image    = value["img"].asString();
    model.type     = value["type"].asString();
    model.favs     = value["favs"].asString();
    model.is_like  = value["is_like"].empty()  ? false : value["is_like"].asBool();
    
    
    if (model.type == "2") {
        model.market_price  = value["market_price"].empty()  ? "" :  value["market_price"].asString();
        model.prices        = value["prices"].empty()        ? "" :  value["prices"].asString();
        model.currency_type = value["currency_type"].empty() ? "" :  value["currency_type"].asString();
        model.describe      = value["title"].empty()         ? "" :  value["title"].asString();
    }
    else if (model.type == "4"){
        model.comments = value["comments"].empty() ? "" :  value["comments"].asString();
        model.hits     = value["hits"].empty()     ? "" :  value["hits"].asString();
        model.describe = value["describe"].empty() ? "" :  value["describe"].asString();
    }
    
    return model;
}


GDHotLabelDetail parseGDHotLabelDetail(const CSJson::Value &value){
   GDHotLabelDetail detail;
    const CSJson::Value& group =  value["group"];
    const CSJson::Value& detailValue =  group["detail"];
	
    detail.id    = detailValue["id"].asString();
    detail.title = detailValue["title"].asString();
    detail.logo  = detailValue["logo"].asString();
    detail.describe  = detailValue["describe"].asString();
    detail.shares    = detailValue["shares"].asString();
	if (detailValue.isMember("type")){
		if (detailValue["type"].isInt()){
			detail.type = CCString::createWithFormat("%d", detailValue["type"].asInt())->m_sString;
		}
		else{
			detail.type = detailValue["type"].asString();
		}
	}
    
    detail.fansCount = detailValue["fans"].asString();
    detail.is_follow = group["is_follow"].asBool();
    
    if (detailValue.isMember("country") && !detailValue["country"].empty() && detailValue["country"] != "") {//国家
        detail.cid       = detailValue["country"]["id"].asString();
        detail.ctitle    = detailValue["country"]["title"].asString();
    }
    
    const CSJson::Value& fans =  group["fans"];
    if (!fans.empty() && fans.size() != 0) {
        vector<GDHotLabelFans> fanValues;
        for (int i = 0; i < fans.size(); i++) {
            const CSJson::Value& itemValue = fans[i];
            GDHotLabelFans fansItem;
            fansItem.uid    = itemValue["uid"].asString();
            fansItem.shares = itemValue["shares"].asString();
            fansItem.add_time  = itemValue["add_time"].asString();
            fansItem.describe  = itemValue["describe"].asString();
            fansItem.is_follow = itemValue["is_follow"].asBool();
            
            const CSJson::Value& user =  itemValue["user"];
            fansItem.username  = user["username"].asString();
            
            fanValues.push_back(fansItem);
        }
        detail.fans = fanValues;
    }
    return detail;
}


